package com.commander;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.Projection;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;

public class NativeNavi extends ReactContextBaseJavaModule {
    public NativeNavi(ReactApplicationContext reactContext) {
        super(reactContext);
    }
    @NonNull
    @Override
    public String getName() {
        return "NativeNavi";
    }
    @ReactMethod
    public void startActivityFromJS(String name, ReadableMap data){
        try {
            Activity activity=getCurrentActivity();
            if(null!=activity){
                Class toActivity = Class.forName(name);
                Intent intent = new Intent(activity,toActivity);
                if(data!=null){
                    intent.putExtra("target",convertReadableMapToBundle(data));
                }
                activity.startActivity(intent);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public  Bundle convertReadableMapToBundle(ReadableMap readableMap) {
        Bundle bundle = new Bundle();
        // 将 ReadableMap 中的数据逐一放入 Bundle
        // 这里假设 ReadableMap 中只包含基本数据类型，你可能需要根据实际情况进行更复杂的处理
        if (readableMap != null) {
            bundle.putDouble("latitude", readableMap.getDouble("latitude"));
            bundle.putDouble("longitude", readableMap.getDouble("longitude"));
            bundle.putString("token", readableMap.getString("token"));
            // 添加其他数据类型...
        }

        return bundle;
    }

}
